var ports = require("../../utils/ports.js")
var app = getApp()
Page({
  data: {
    HotwordList: [],
    SearchHistory: [],
    keywords: "",
    HotKeyID: "",
    goodsList: [],
    showpromodel: false
  },
  onLoad(o) {
    var that = this
    that.setData({
      thisBotH: '0'
    })
    var SearchHistory = wx.getStorageSync("SearchHistory");
    if (SearchHistory) {
      this.setData({
        SearchHistory: SearchHistory.reverse()
      })
    }
    that.getHotwordList()
  },
  SearchInput(e) {
    var that = this
    var keywords = e.detail.value
    that.setData({
      keywords: keywords,
      goodsList: []
    })
    if (keywords.trim() != '') {
      that.getGoodsShopList(keywords)
    }
  },
  getGoodsShopList(keywords) {
    var that = this
    ports.ModuleGoodsShop.getGoodsShopList({
      keyWords: keywords
    }, that, res => {
      if (res.data.header.code == 0) {
        if (res.data.body.data.rows.length > 0) {
          that.setData({
            haveData: true,
            goodsList: res.data.body.data.rows
          })
        } else {
          that.setData({
            haveData: false
          })
        }
        that.addhistory(keywords)
      } else {
        wx.showToast({
          title: res.data.header.msg,
          icon: 'none'
        })
      }
    })
  },
  // 删除一个历史记录
  DeleteHistory(e) {
    var index = e.currentTarget.dataset.index;
    var SearchHistory = this.data.SearchHistory;
    SearchHistory.splice(index, 1)
    this.setData({
      SearchHistory
    })
    wx.setStorageSync("SearchHistory", SearchHistory)
  },
  DeleteAll() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您确定删除所有搜索记录吗',
      success(res) {
        if (res.confirm) {
          that.setData({
            SearchHistory: []
          })
          wx.setStorageSync("SearchHistory", [])
        }
      }
    })
  },
  // 一个热门搜索或者历史搜索
  onesearch(e) {
    var that = this
    var keywords = e.currentTarget.dataset.keywords;
    app.globalData.keywords = keywords
    that.setData({
      keywords: keywords
    })
    that.getGoodsShopList(keywords)
  },
  getHotwordList() {
    var that = this
    var req = {};
    req.searchDefineID = app.searchDefineID
    req.siteID = app.siteID
    ports.ModuleSearch.getHotWordList(req, that, res => {
      if (res.data.header.code == 0) {
        that.setData({
          HotwordList: res.data.body.data.rows
        })
      }
    })
  },
  addhistory(keywords) {
    var SearchHistory = wx.getStorageSync("SearchHistory");
    var cando = true;
    if (!SearchHistory) SearchHistory = [];
    for (var item of SearchHistory) {
      if (item.name == keywords) {
        cando = false
      }
    }
    if (cando) {
      SearchHistory.push({
        name: keywords
      })
      wx.setStorageSync("SearchHistory", SearchHistory)
    }
  },
  //商品详情
  toGoodsDetail(e) {
    wx.navigateTo({
      url: '/pages/proInfo/proInfo?id=' + e.currentTarget.dataset.id,
    })
  },
  //隐藏
  hide_black: function (e) {
    this.setData({
      toggleGoodsFeature: false
    })
  },

  //商品详情
  toGoodsDetail(e) {
    //console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/proInfo/proInfo?id=' + e.currentTarget.dataset.id,
    })
  },
  //加入购物车，立即购买动画
  setSubmitText: function (e) {
    var that = this
    that.setData({
      submittext: e.currentTarget.dataset.todo == '1' ? true : false,
      amount: 1
    })
    var id = e.currentTarget.dataset.id
    var memberID = wx.getStorageSync('USER_MEMBERID')
    if (id) {
      that.setData({
        goodsShopID: id
      })
      if (memberID != null) {
        that.getGoodsShopDetail(memberID)
      } else {
        wx.showToast({
          title: '请登录',
          icon: 'none'
        })
      }
    } else {
      wx.showToast({
        title: '参数异常',
        icon: 'none'
      })
      setTimeout(function () {
        wx.navigateBack()
      }, 1000)
    }
  },
  //商品详情
  getGoodsShopDetail: function (memberID) {
    var that = this
    //店铺商品详情
    ports.ModuleGoodsShop.getGoodsShopDetail({
      goodsShopID: that.data.goodsShopID,
      type: 2,
      memberID: memberID
    }, that, res => {
      if (res.data.header.code == 0) {
        that.setData({
          goodsShop: res.data.body.data,
          gshowPrice: res.data.body.data.realPrice
        })
        if (res.data.body.data.isCollect == 0) {
          that.setData({
            collected: false
          })
        } else {
          that.setData({
            collected: true
          })
        }
        that.getFeatureList(that.data.goodsShopID);
      }
    })

    //获取订单运费
    ports.ModuleGoodsShop.getParameterValueByCode({
      code: app.orderFare,
    }, that, res => {
      if (res.data.header.code == 0) {
        that.setData({
          orderFare: res.data.body.data
        })
      }
    })

    //获取包邮信息
    ports.ModuleGoodsShop.getParameterValueByCode({
      code: app.orderAmountFreeFare,
    }, that, res => {
      if (res.data.header.code == 0) {
        that.setData({
          orderAmountFreeFare: res.data.body.data
        })
      }
    })
  },
  //获取商品规格
  getFeatureList(objectID) {
    var that = this
    //获取产品规格   
    ports.ModuleGoodsShop.getObjectFeatureItemList({
      objectID: objectID,
      pageNumber: 999
    }, that, res => {
      if (res.data.header.code == 0) {
        that.setData({
          featureItemList: res.data.body.data
        })
        if (that.data.featureItemList.length > 0) {
          that.setData({
            //默认选中第一个规格
            fitemName: that.data.featureItemList[0].itemList[0].objectFeatureItemName,
            fid: that.data.featureItemList[0].itemList[0].objectFeatureItemID
          })
        }
        that.setData({
          toggleGoodsFeature: true,
          showpromodel: true
        })
      }
    })
  },

})